package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.domain.SysFile;
import com.ssm.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 文件控制类
 */
@Controller
@RequestMapping("/file")
public class FileController {

    @Autowired
    private IFileService fileService;

    /**
     * 根据id查询文件
     * @param fileId
     * @return
     * @throws Exception
     */
    @RequestMapping("/findById.do")
    public ModelAndView findById(@RequestParam(name="id", required = true)String fileId) throws Exception {
        ModelAndView mv = new ModelAndView();
        SysFile sysFile = fileService.findById(fileId);
        //添加模型数据，将对象添加到模型中
        mv.addObject("file", sysFile);
        //设置逻辑视图名
        mv.setViewName("file-show");
        //返回ModelAndView对象
        return mv;
    }

    /**
     * 下载文件
     * @param fileId
     * @throws Exception
     */
    @RequestMapping("/downloadFile.do")
    public void downloadFile(@RequestParam(name="id", required = true) String fileId) throws Exception {
        fileService.downloadFile(fileId);
    }

    /**
     * 文件上传
     * @param files
     * @return
     * @throws Exception
     */
    @RequestMapping("/upload.do")
    public String upload(@RequestParam("file") MultipartFile[] files) throws Exception {

        for (MultipartFile file: files) {
            System.out.println(file.getOriginalFilename() + "  " +file.getSize());
        }
        fileService.save(files);
        return "redirect:findAll.do";
    }

    /**
     * 删除文件
     * @param fileId
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteFile.do")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String deleteFile(@RequestParam(name="id", required = true)String fileId) throws Exception {
        fileService.deleteFile(fileId);
        return "redirect:findAll.do";
    }

    /**
     * 查看所有文件
     * @param page
     * @param size
     * @return
     * @throws Exception
     */
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name="page",required = true, defaultValue = "1") Integer page,
                                @RequestParam(name="size",required = true, defaultValue = "5") Integer size)
            throws Exception {
        ModelAndView mv = new ModelAndView();
        List<SysFile> fileList = fileService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(fileList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("file-list");
        return mv;
    }


    /**
     * 模糊查询
     * @param page
     * @param size
     * @param keyWord
     * @return
     * @throws Exception
     */
    @RequestMapping("/select.do")
    public ModelAndView selectFile(@RequestParam(name="page",required = true, defaultValue = "1") Integer page,
                                   @RequestParam(name="size",required = true, defaultValue = "5") Integer size,
                                   @RequestParam(name="keyWord",defaultValue = "") String keyWord)
            throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.addObject("keyWord", keyWord);
        List<SysFile> fileList = fileService.selectFile(page,size,keyWord);
        PageInfo pageInfo = new PageInfo(fileList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("file-list-select");
        return mv;
    }

}
